﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 策略模式
{
    public partial class Form1 : Form
    {
        double total = 0;
        public Form1()
        {
            InitializeComponent();
            combox.Items.AddRange(new object[] { "正常收费", "满300返100", "打8折" });
            combox.SelectedIndex = 0;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            CashContext csuper = new CashContext(combox.SelectedItem.ToString());
            double totalPrice = 0d;
            totalPrice = csuper.GetResult(double.Parse(txtPrice.Text) * double.Parse(txtCount.Text));
            total = total + totalPrice;
            list.Items.Add("单价：" + txtPrice.Text + "数量：" + txtCount.Text + " " + combox.SelectedItem.ToString() + "总价：" + totalPrice);
            lblResult.Text = total.ToString();
        }

        private void tnRest_Click(object sender, EventArgs e)
        {

        }
    }

    //现金收费抽象类
    abstract class CashSuper
    {
        //money原价
        public abstract double AcceptCash(double money);
    }

    #region 策略模式子类
    //正常收费子类
    class CashNormal : CashSuper
    {
        public override double AcceptCash(double money)
        {
            return money;
        }
    }

    //打折收费子类
    class CashRebate : CashSuper
    {
        private double moneyRebate = 1d;
        public CashRebate(string moneyRebate)
        {
            this.moneyRebate = double.Parse(moneyRebate);
        }

        public override double AcceptCash(double money)
        {
            return money * this.moneyRebate;
        }
    }

    //返利收费子类
    class CashReturn : CashSuper
    {
        private double moneyCondition = 0.0d;
        private double moneyReturn = 0.0d;
        public CashReturn(string moneyCondition, string moneyReturn)
        {
            this.moneyReturn = double.Parse(moneyReturn);
            this.moneyCondition = double.Parse(moneyCondition);
        }

        public override double AcceptCash(double money)
        {
            double result = money;
            if (money > moneyCondition)
                result = money - Math.Floor(money / moneyCondition) * moneyReturn;
            return result;
        }
    }
    #endregion
    
    //CashContext类
    class CashContext
    {
        CashSuper cs = null;
        public CashContext(string type)
        {
            switch (type)
            {
                case "满300返100":
                    cs = new CashReturn("300", "100");
                    break;
                case "打8折":
                    cs = new CashRebate("0.8");
                    break;
                default:
                    cs = new CashNormal();
                    break;
            }
        }
        public double GetResult(double money)
        {
            return cs.AcceptCash(money);
        }
    }
}
